package game

import (
	"errors"
	"fmt"
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/game"
	"gorm.io/gorm"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateApi
//@description: 新增基础api
//@param: api model.SysApi
//@return: err error

type ProjectGameInfoService struct{}

var ProjectGameInfoServiceApp = new(ProjectGameInfoService)

func (service *ProjectGameInfoService) CreateProjectGameInfo(param game.BsProjectGameInfo) (err error) {
	return global.GVA_DB.Create(&param).Error
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteApi
//@description: 删除基础api
//@param: api model.SysApi
//@return: err error

func (service *ProjectGameInfoService) DeleteProjectGameInfo(param game.BsProjectGameInfo) (err error) {
	var entity game.BsProjectGameInfo
	err = global.GVA_DB.First(&entity, "project_id = ? AND game_info_id = ?", param.ProjectId, param.GameInfoId).Error // 根据id查询api记录
	if errors.Is(err, gorm.ErrRecordNotFound) {                                                                        // api记录不存在
		return err
	}
	err = global.GVA_DB.Delete(&entity).Error
	if err != nil {
		return err
	}
	return nil
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetAPIInfoList
//@description: 分页获取数据,
//@param: api model.SysApi, info request.PageInfo, order string, desc bool
//@return: list interface{}, total int64, err error

func (service *ProjectGameInfoService) GetProjectGameInfoList(param game.BsProjectGameInfo, info request.PageInfo, order string, desc bool, datascope common.DataScope) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.GVA_DB.Model(&game.BsProjectGameInfo{}).Select("bs_project_game_info.*, bs_game_info.title AS game_info_title, bs_project.title AS project_title, bs_safety_known.content AS safety_known_content")
	db = db.Joins("LEFT JOIN bs_game_info ON bs_project_game_info.game_info_id = bs_game_info.id")
	db = db.Joins("LEFT JOIN bs_project ON bs_project_game_info.project_id = bs_project.id")
	db = db.Joins("LEFT JOIN bs_safety_known ON bs_game_info.safety_id = bs_safety_known.id")
	var giList []game.BsProjectGameInfo
	if datascope.AuthorityId == 888 {
		db = db.Joins("INNER JOIN bs_user_game_info ON bs_user_game_info.game_info_id = bs_project_game_info.game_info_id AND bs_user_game_info.user_id = ?", datascope.UserId)
	} else if datascope.AuthorityId == 9528 {
		db = db.Joins("INNER JOIN bs_sign_up ON bs_sign_up.game_info_id = bs_project_game_info.game_info_id AND bs_sign_up.project_id = bs_project_game_info.project_id AND bs_sign_up.user_id = ?", datascope.UserId)
	}
	if param.Status > 0 {
		db = db.Where("bs_sign_up.status = ?", param.Status)
	}
	if param.ProjectId > 0 {
		db = db.Where("bs_project_game_info.project_id = ?", param.ProjectId)
	}
	if param.ProjectTitle != "" {
		db = db.Where("bs_project.title LIKE %?%", param.ProjectTitle)
	}
	if param.GameInfoTitle != "" {
		db = db.Where("bs_game_info.title LIKE %?%", param.GameInfoTitle)
	}

	err = db.Count(&total).Error

	if err != nil {
		return giList, total, err
	}

	db = db.Limit(limit).Offset(offset)
	OrderStr := "create_time desc"
	if order != "" {
		orderMap := make(map[string]bool, 2)
		orderMap["update_time"] = true
		orderMap["create_time"] = true
		if !orderMap[order] {
			err = fmt.Errorf("非法的排序字段: %v", order)
			return giList, total, err
		}
		OrderStr = order
		if desc {
			OrderStr = order + " desc"
		}
	}
	err = db.Order(OrderStr).Find(&giList).Error
	for i := 0; i < len(giList); i++ {
		if giList[i].ContestStartTime.After(time.Now()) {
			giList[i].GameStatus = 1
		} else if giList[i].ContestEndTime.After(time.Now()) {
			giList[i].GameStatus = 2
		} else {
			giList[i].GameStatus = 3
		}
	}
	return giList, total, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetApiById
//@description: 根据id获取api
//@param: id float64
//@return: api model.SysApi, err error

func (service *ProjectGameInfoService) GetProjectGameInfoById(projectId, gameInfoId int) (gi game.BsProjectGameInfo, err error) {
	err = global.GVA_DB.First(&gi, "project_id = ? AND game_info_id = ?", projectId, gameInfoId).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateApi
//@description: 根据id更新api
//@param: api model.SysApi
//@return: err error

func (service *ProjectGameInfoService) UpdateProjectGameInfo(gi game.BsProjectGameInfo) (err error) {
	return global.GVA_DB.Save(&gi).Error
}
